// pages/aQuan/aQuan.js
var service_common = require("../../service/common.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    openid:"",
    types:[],
    inds:-2,
    class_id:-2,
    roomList:[],
    page:1,
    offon:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      openid: getApp().globalData.userInfo.openid,
    })
    this.getMyRoom()
  },
  // 前往房间
  goRoom:function(e){
    wx.navigateTo({
      url: '/pages/aHome/aHome?id=' + e.currentTarget.dataset.id,
    })
  },
  // 获取分类列表
  getroomTypes:function(){
    service_common.fetchData(
      service_common.getUrl("user.roomTypes"),
      {
        openid: this.data.openid
      },
      (res) => {
        if(res.data.code == 0){
          this.setData({
            types: res.data.data
        })
        }
      },
      "GET"
    )
  },
  goCreateHome:function(){
    wx.navigateTo({
      url: '/pages/aCreateHome/aCreateHome',
    })
  },
  // 搜索房间
  searchRoom:function(e){
    service_common.fetchData(
      service_common.getUrl("user.searchRoom"),
      {
        openid: this.data.openid,
        code: e.detail.value
      },
      (res) => {
        if (res.data.code == 0) {
          wx.navigateTo({
            url: '/pages/aHome/aHome?id=' + res.data.data.id,
          })
        }else{
          wx.showToast({
            title: '未搜索到该房间',
            image: '/image/cancel.png',
            duration: 2000
          });
        }
      },
      "GET"
    )
  },
  // 列表触底操作
  lower:function(){
    if (this.data.offon){
      this.setData({
        offon:false,
        page: this.data.page+=1
      })
      switch (this.data.inds) {
        case -2:
          //我的房间
          this.getMyRoom()
          break;
        case -1:
          //我加入的房间
          this.getMyJoinRooms()
          break;
        default:
          this.getHomeList()
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getroomTypes()
    
  },
// 点击分类
  act_:function(e){
    if (e.currentTarget.dataset.index!=this.data.inds){
      this.setData({
        inds: e.currentTarget.dataset.index,
        class_id:e.currentTarget.dataset.id,
        page:1,
        offon:true
      })
      switch (this.data.inds) {
        case '-2':
          //我的房间
          this.getMyRoom()
          break;
        case '-1':
          //我加入的房间
          this.getMyJoinRooms()
          break;
        default:
          this.getHomeList()
      }
    }
    
  },
  // 获取我的房间
  getMyRoom:function(){
    service_common.fetchData(
      service_common.getUrl("user.getMyRoom"),
      {
        openid: this.data.openid,
        page: this.data.page
      },
      (res) => {
        if (res.data.code == 0) {
          if(this.data.page == 1){
            for(let i in res.data.data){
              if (res.data.data[i].logo.indexOf('aliyuncs')!=-1){
                res.data.data[i].logo = res.data.data[i].logo +"?x-oss-process=image/resize,h_100"
              }
              if (res.data.data[i].src.indexOf('aliyuncs')!=-1){
                res.data.data[i].src = res.data.data[i].src + "?x-oss-process=image/resize,h_50"
              }
            }
            this.setData({
              roomList:res.data.data
            })
          }else{
            if (res.data.data.length == 0){
              wx.showToast({
                title: '数据加载完成...',
              })
            }else{
              for (let i in res.data.data) {
                if (res.data.data[i].logo.indexOf('aliyuncs') != -1) {
                  res.data.data[i].logo = res.data.data[i].logo + "?x-oss-process=image/resize,h_100"
                }
                if (res.data.data[i].src.indexOf('aliyuncs') != -1) {
                  res.data.data[i].src = res.data.data[i].src + "?x-oss-process=image/resize,h_50"
                }
              }
              this.data.roomList = this.data.roomList.concat(res.data.data)
              this.setData({
                roomList: this.data.roomList,
                offon:true
              })
            }
          }
        }
      },
      "GET"
    )
  },
  // 获取我加入的房间
  getMyJoinRooms: function () {
    service_common.fetchData(
      service_common.getUrl("user.getMyJoinRooms"),
      {
        openid: this.data.openid,
        page: this.data.page
      },
      (res) => {
        if (res.data.code == 0) {
          if (this.data.page == 1) {
            for (let i in res.data.data) {
              if (res.data.data[i].logo.indexOf('aliyuncs') != -1) {
                res.data.data[i].logo = res.data.data[i].logo + "?x-oss-process=image/resize,h_100"
              }
              if (res.data.data[i].src.indexOf('aliyuncs') != -1) {
                res.data.data[i].src = res.data.data[i].src + "?x-oss-process=image/resize,h_50"
              }
            }
            this.setData({
              roomList: res.data.data
            })
          } else {
            if (res.data.data.length == 0) {
              wx.showToast({
                title: '数据加载完成...',
              })
            } else {
              for (let i in res.data.data) {
                if (res.data.data[i].logo.indexOf('aliyuncs') != -1) {
                  res.data.data[i].logo = res.data.data[i].logo + "?x-oss-process=image/resize,h_100"
                }
                if (res.data.data[i].src.indexOf('aliyuncs') != -1) {
                  res.data.data[i].src = res.data.data[i].src + "?x-oss-process=image/resize,h_50"
                }
              }
              this.data.roomList = this.data.roomList.concat(res.data.data)
              this.setData({
                roomList: this.data.roomList,
                offon: true
              })
            }
          }
        }
      },
      "GET"
    )
  },
  // 获取其他房间
  getHomeList:function(){
    service_common.fetchData(
      service_common.getUrl("user.getRoomsByClassId"),
      {
        openid: this.data.openid,
        page: this.data.page,
        class_id: this.data.class_id
      },
      (res) => {
        if (res.data.code == 0) {
          if (this.data.page == 1) {
            for (let i in res.data.data) {
              if (res.data.data[i].logo.indexOf('aliyuncs') != -1) {
                res.data.data[i].logo = res.data.data[i].logo + "?x-oss-process=image/resize,h_100"
              }
              if (res.data.data[i].src.indexOf('aliyuncs') != -1) {
                res.data.data[i].src = res.data.data[i].src + "?x-oss-process=image/resize,h_50"
              }
            }
            this.setData({
              roomList: res.data.data
            })
          } else {
            if (res.data.data.length == 0) {
              wx.showToast({
                title: '数据加载完成...',
              })
            } else {
              for (let i in res.data.data) {
                if (res.data.data[i].logo.indexOf('aliyuncs') != -1) {
                  res.data.data[i].logo = res.data.data[i].logo + "?x-oss-process=image/resize,h_100"
                }
                if (res.data.data[i].src.indexOf('aliyuncs') != -1) {
                  res.data.data[i].src = res.data.data[i].src + "?x-oss-process=image/resize,h_50"
                }
              }
              this.data.roomList = this.data.roomList.concat(res.data.data)
              this.setData({
                roomList: this.data.roomList,
                offon: true
              })
            }
          }
        }
      },
      "GET"
    )
  },
  // 创建更多房间
  createHome: function () {
    if (getApp().globalData.userInfo.is_member == 0) {
      wx.showToast({
        title: '大使vip以上才能创建房间',
        image: '/image/cancel.png',
        duration: 2000
      });
    } else {
      // api.openWin({
      //   name: 'Peng.createType',
      //   url: './createType.html'
      // });
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})